<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/27 0027
 * Time: 14:13
 */

namespace Api\Controller;


class RedPacketController extends BaseController
{
    protected $mid;
    protected $member;
    public function __construct()
    {
        parent::__construct();
        //验证用户
        $openid = I('request.openid','');
        empty($openid) && $this->error('openid缺失！');
        $member = M('Usermember')->where(['openid' => $openid])->find();
        empty($member) && $this->error('用户不存在！');
        $this->mid = $member['id'];
    }
    public function index()
    {

    }
    public function save()
    {
        $id = I('post.id');// 红包订单ID
        $lat = I('post.lat');
        $lng = I('post.lng');
        $red_packet = M('AdOrderList')->where(['id' => $id,'ad_type' => '2', 'deploy_status' => '1'])->find();
        empty($red_packet) && $this->error('请求错误！');
        if($red_packet['red_packet_num'] < 1){
            $this->error('红包已抢光！');
        }
        if(M('RedRecord')->where(array('ad_id'=>$id,'user_id'=>$this->mid))->count() > 0){
            $this->error('您已领取过该红包！');
        }
        $shop_info = M('Shop')->where(['id' => $red_packet['shop_id'],'status' =>'2'])->find();
        empty($shop_info) && $this->error('商家不存在或已被禁用！');
        //查询投放者商家信息
        $self_shop = M('Shop')->where(['user_id' => $red_packet['mid'],'status' => '2'])->find();
        empty($self_shop) && $this->error('投放商家信息错误！');

        //领取距离判定
        if($red_packet['distance'] > 0){
            empty($lat) && $this->error('维度缺失！');
            empty($lng) && $this->error('经度缺失！');
            $distance = distance($lat,$lng,$red_packet['lat'],$red_packet['lng']);
            if($distance > $red_packet['distance']){
                $this->error('超出领取距离限制！');
            }
        }


        //开始领取红包
        $user_ratio = C('IN_USER_LIMIT');
        $shop_ratio = C('IN_SHOP_LIMIT');
        $user_red_packet_amount = floor($red_packet['red_packet_moeny'] * $user_ratio * 100)/100;
        $shop_red_packet_amount = floor($red_packet['red_packet_moeny'] * $shop_ratio * 100)/100;
        M()->startTrans();// 开启事务
        //扣除红包领取次数
        $order_list_result = M('AdOrderList')->where(['id' => $id])->setDec('red_packet_num',1);
        //添加用户余额
        $usermember_result = M('Usermember')->where(['id' => $this->mid])->setInc('balance',$user_red_packet_amount);
        //添加用户领取记录
        $urecord['ad_id']=$id;
        $urecord['user_id']=$this->mid;
        $urecord['money']=$user_red_packet_amount;
        $urecord['addtime']=time();
        $add_red_record = M('red_record')->add($urecord);

        //添加被投商户余额
        $shop_save_data = [
            'balance' => $shop_info['balance'] + $shop_red_packet_amount,
            'shop_balance' => $shop_info['shop_balance'] + $shop_red_packet_amount,
            'exposure_balance' => $shop_info['exposure_balance'] + $shop_red_packet_amount,
            'withdraw_balance' => $shop_info['withdraw_balance'] + $shop_red_packet_amount
        ];
        $shop_result = M('Shop')->where(['id' => $red_packet['shop_id']])->save($shop_save_data);
        //给商户添加用户扫码记录
        $qrcode['user_id'] = $this->mid;
        $qrcode['shop_id'] = $self_shop['id']; //商户id 具体门店增加字段store_id
        $qrcode['ad_type'] = $red_packet['ad_type'];
        $qrcode['title'] = '用户扫码领取红包记录';
        $qrcode['addtime'] = time();
        $add_shop_qrcode_record = M('ShopQrcodeRecord')->add($qrcode);
        // 添加商户粉丝
        $add_shop_fans_result = M('ShopFans')->add([
            'mid' => $this->mid,
            'sid' => $self_shop['id'],
            'order_list_id' => $id
        ]);
        if($order_list_result !== false &&
            $usermember_result !== false &&
            $add_red_record !== false &&
            $shop_result !== false &&
            $add_shop_qrcode_record !== false &&
            $add_shop_fans_result !== false
        ){
            M()->commit();
            $this->success('红包领取成功！',['price' => $user_red_packet_amount]);
        } else {
            M()->rollback();
            $this->error('红包领取失败！');
        }
    }
}